package com.lsabcd.dubbo.test;

import org.springframework.util.CollectionUtils;

import java.util.HashMap;
import java.util.LinkedList;
import java.util.List;
import java.util.Map;
import java.util.concurrent.atomic.AtomicInteger;

/**
 * @author lidingbo
 * @date 2020/7/30 15:35
 * @description
 */
public class WindowContainer {
    public void submit(WindowItem item){
        if (currentWindow == null) {
            synchronized (currentWindowLock) {
                if (currentWindow == null) {
                    currentWindow = new Window(interval,item.getTime().getTime());
                    container.add(currentWindow);
                }
            }
        }
        if (item.getTime().getTime() - currentWindow.getStartTime() > windowSize) {
            synchronized (currentWindowLock) {
                currentWindow = new Window(interval,item.getTime().getTime());
                container.add(currentWindow);
            }
        }
        currentWindow.addItem(item);
    }
    public Map<String,WindowItem> statistics(){
        Long now = System.currentTimeMillis();
        LinkedList<WindowItem> items = new LinkedList<>();

        int len = container.size();
        for (int i = 0; i < len; i++) {
            Window c = container.get(i);
            if (now - c.getStartTime() > interval) {
                continue;
            }
            if (now - c.getStartTime() < 0) {
                break;
            }
            for (WindowItem it : c.getList()) {
                insert(items,it);
            }
        }
        Map<String, WindowItem> map = new HashMap<>();
        int count = items.size();
        if (count == 0) {
            return map;
        }
        int indexTP90 = (int) (count * 0.9);
        int indexTP99 = (int) (count * 0.99);
        WindowItem itemTP90 = items.get(indexTP90);
        WindowItem itemTP99 = items.get(indexTP99);

        map.put("TP90", itemTP90);
        map.put("TP99", itemTP99);
        return map;
    }

    private void insert(LinkedList<WindowItem> list,WindowItem item){
        if (CollectionUtils.isEmpty(list)) {
            list.add(item);
            return;
        }
        int index = -1;
        for (int i = 0; i < list.size(); i++) {
            if (item.getValue() > list.get(i).getValue()) {
                index = i;
                break;
            }
        }

        if (index == -1) {
            list.addLast( item);
        } else if (index == 0) {
            list.addFirst(item);
        } else {
            list.add(index-1,item);
        }

    }

    public static void main(String[] args) {
        LinkedList<Integer> list = new LinkedList<>();
        list.add(1);
        list.add(2);
        list.add(3);
        list.add(4);
        list.add(5);

        list.add(5,100);
        System.out.println(list);
    }


    // ms
    private static final int interval = 60000;
    private static final int windowSize = 5000;


    private List<Window> container = new LinkedList<>();

    private Window currentWindow = null;
    private Object currentWindowLock = new Object();
    public List<Window> getContainer() {
        return container;
    }

    public void setContainer(List<Window> container) {
        this.container = container;
    }

    public Window getCurrentWindow() {
        return currentWindow;
    }

    public void setCurrentWindow(Window currentWindow) {
        this.currentWindow = currentWindow;
    }

}
